<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\ChatKey;
use App\Repository\ArticleRepository;
use App\Repository\ChatKeyRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiChatKeyController extends BaseController4Restful
{
    /**
     * @Route("/api/chatKeys", name="apiGetChatKeys",methods={"GET","POST"})
     */
    public function apiGetChatKeys(ChatKeyRepository $repository, Request $request,ArticleRepository $articleRepository)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $ChatKey ChatKey */
                $ChatKey = $this->createEntity($data, ChatKey::class);
                //更新文章数据
                $str = $ChatKey->getReply();
                $arr = $this->myExplodeNumbers($str);
                foreach ($arr as $id) {
                    $art = $articleRepository->find($id);
                    if ($art) {
                        $ChatKey->addArt($art);
                    }
                }
                $this->myEm()->persist($ChatKey);
                $this->myEm()->flush();
                return $this->json_out(200, "创建成功", $ChatKey);
            default:
                if (!$page = $this->getParamRequired("page")) {
                    $page = 1;
                }
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page, $total, $limit);
                $list = $repository->findBy(array(), array(), $limit, $offset);
                return $this->json_out(200, "获取成功", $list);
        }
    }

    /**
     * @Route("/api/chatKeys/{id}", name="apiAddChatKeys",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateChatKeys(ChatKey $ChatKey, Request $request, ArticleRepository $articleRepository)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($ChatKey);
                $this->myEm()->flush();
                return $this->json_out(200, "已移除");
            case "PATCH":
            case "PUT":
                /**@var $ChatKey ChatKey */
                $ChatKey = $this->updateEntity($data, ChatKey::class, $ChatKey);
                //更新文章数据
                $str = $ChatKey->getReply();
                $arr = $this->myExplodeNumbers($str);
                foreach ($arr as $id) {
                    $art = $articleRepository->find($id);
                    if ($art) {
                        $ChatKey->addArt($art);
                    }
                }
                $this->myEm()->persist($ChatKey);
                $this->myEm()->flush();
                return $this->json_out(200, "保存", $ChatKey);
                break;
            case "Get":
            default:
        }
//        $this->myEm()->persist($ChatKey);
//        $this->myEm()->flush();
        return $this->json_out(200, "获取成功", $ChatKey);
    }
}
